package com.tankwar.view.domain;

import java.util.ArrayList;
import java.util.List;

import com.swak.fx.support.Display;
import com.tankwar.core.Arena;
import com.tankwar.core.DrawAble;
import com.tankwar.core.RectDrawAble;
import com.tankwar.view.resources.Images;

import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.Image;
import javafx.scene.paint.Color;

/**
 * 地图
 * 
 * @author 618lf
 */
public class Map extends RectDrawAble {

	private Arena arena;

	/**
	 * 所有的可绘制对象
	 */
	private List<RectDrawAble> drawAbles = new ArrayList<>();

	public Map(Arena arena) {
		super(arena.getWidth() * 2, arena.getHeight() * 2);
		this.arena = arena;
		Wall wall = new Wall(500, 500, new Image(Display.load(Images.BRICKWALL_IMAGE_URL).toExternalForm()));
		this.drawAbles.add(wall);
	}

	@Override
	public void draw(GraphicsContext gc) {
		gc.setFill(Color.BLACK);
		gc.fillRect(this.getX(), this.getY(), getWidth(), getHeight());

		// 画背景
		for (int i = 0; i < drawAbles.size(); i++) {
			DrawAble wObject = drawAbles.get(i);
			wObject.draw(gc);
		}
	}

	/**
	 * 整体的背景元素同步移动
	 * 
	 * @param offsetX
	 * @param offsetY
	 */
	protected void moveBy(double offsetX, double offsetY) {
		this.movebyX(offsetX);
		this.movebyY(offsetY);
		for (int i = 0; i < drawAbles.size(); i++) {
			RectDrawAble wObject = drawAbles.get(i);
			wObject.movebyX(offsetX);
			wObject.movebyY(offsetY);
		}
	}

	/**
	 * 移动地图
	 * 
	 * @param offsetX
	 * @param offsetY
	 */
	public void move(double offsetX, double offsetY) {
		double x = this.getX() - offsetX;
		double y = this.getY() - offsetY;
		if (x <= 0 && y <= 0 && x + this.getWidth() >= this.arena.getWidth()
				&& y + this.getHeight() >= this.arena.getHeight()) {
			this.moveBy(-offsetX, -offsetY);
		}
	}
}
